package com.woniu.service.impl;

import com.woniu.adapter.UserAdapter;
import com.woniu.outlet.dao.UserPoMapper;
import com.woniu.outlet.po.UserPo;
import com.woniu.outlet.po.UserPoExample;
import com.woniu.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private UserAdapter userAdapter;

    @Autowired
    private UserPoMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public List<UserPo> findUsers() throws Exception {
        List<UserPo> users = userAdapter.findUsers();
        return users;
    }

    @Override
    public UserPo getUserById(Integer id) throws Exception {
        return userAdapter.getUserById(id);
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserPoExample example = new UserPoExample();
        //创建一个条件
        UserPoExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(username);
        //查询用户
        List<UserPo> userList = userMapper.selectByExample(example);
        if (userList == null || userList.size() == 0) {
            return null;
        } else {
            //创建一个UserDetails实例
            UserPo user = userList.get(0);

            List<String> perCodes = userMapper.selectPercodeByPerm(user.getUsername());
            //创建存放权限的集合 --暂时为空集合
            List<GrantedAuthority> authorities = new ArrayList<>();

            ValueOperations ops =  redisTemplate.opsForValue();
            ops.set(user.getUsername(),perCodes);

            perCodes.forEach(percode->{
                authorities.add(new SimpleGrantedAuthority(percode));
            });

            UserDetails userDetails = null;
            if (user.getStatus().equals("0")){
                userDetails = new org.springframework.security.core.userdetails.User(user.getUsername(),
                        user.getPassword(),true,true,true,
                        user.getStatus().equals("1")?false:true,authorities);
            }else{
                userDetails = new org.springframework.security.core.userdetails.User(user.getUsername(),
                        user.getPassword(),authorities);
            }


            //根据数据库查询信息，封装UserDetails对象，并返回
//            UserDetails userDetails =
//                    new org.springframework.security.core.userdetails.User(user.getUsername(),
//                            user.getPassword(),true,true,true,
//                            user.getStatus().equals("1")?false:true,authorities);
            return userDetails;
        }
    }
}
